using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AsteriosSpwan : MonoBehaviour
{
    [SerializeField]
    private Asteriod asteriodPrefabs;
    [SerializeField]
    private float spawnRate;
    [SerializeField]
    private int SpawnCount;
    private readonly float spawnOffset = 30f;
    private readonly float trajectoryVariance = 20f;
    void Start()
    {
        InvokeRepeating(nameof(Spawn), 0f, spawnRate);
    }

    private void Spawn()
    {
        for (int i = 0; i < SpawnCount; i++)
        {
            //在单位圆内生成一个随机方向向量（2D），并归一化为单位长度。将方向向量缩放为实际生成半径。
            Vector3 spawnDirection = Random.insideUnitCircle.normalized * spawnOffset;
            //生成点位于生成器对象的周围圆形区域。
            Vector3 spawnPoint = transform.position + spawnDirection;

            //指定一个范围内的随机角度
            float variance = Random.Range(-trajectoryVariance, trajectoryVariance);
            //创建一个绕Z旋转variance度的四元数
            Quaternion rotation = Quaternion.AngleAxis(variance, Vector3.forward);

            //实例化预制体 指定生成位置 和旋转角度
            Asteriod asteriod = Instantiate(asteriodPrefabs, spawnPoint, rotation);
            //指定随机大小
            asteriod._size = Random.Range(asteriod._mixSize, asteriod._maxSize);

            //调用移动方法
            asteriod.SetTrajectory(rotation * -spawnDirection);
        }
    }
}
